//https://leetcode.cn/problems/minimum-size-subarray-sum/
package codeRandomThoughts.Test209长度最小的子数组;

//二刷
public class Solution2 {

    public int minSubArrayLen(int target, int[] nums) {
        //滑动窗口法
        //首先定义窗口左右两侧指针
        int l = 0;
        int r = 0;

        //返回值 如果不存在符合条件的子数组,需要返回0
        int minLen = nums.length + 1;
        //窗口内的和
        int sum = 0;

        for (r = 0; r < nums.length; r++) {
            sum += nums[r];
            while (sum >= target) {
                //更新最小长度
                minLen = Math.min(minLen, r - l + 1);
                sum -= nums[l];
                l++;
            }
        }

        return minLen == nums.length + 1 ? 0 : minLen;
    }
}
